package com.shure.surdes.common.utils;

import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @author shure
 * @description
 * @date 2023/1/10
 */
@Slf4j
public class TimeUtil {

    private static String CurrentTime;
    private static String CurrentDate;

    public TimeUtil() {
    }

    public static String getCurrentYear() {
        Date nowDate = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
        return formatter.format(nowDate);
    }

    public static String getCurrentMonth() {
        Date nowDate = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("MM");
        return formatter.format(nowDate);
    }

    public static String getCurrentDay() {
        Date nowDate = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("dd");
        return formatter.format(nowDate);
    }

    public static String getCurrentTime() {
        Date nowDate = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        CurrentTime = formatter.format(nowDate);
        return CurrentTime;
    }

    public static String getCurrentCompactTime() {
        Date nowDate = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        CurrentTime = formatter.format(nowDate);
        return CurrentTime;
    }

    public static String getYesterdayCompactTime() {
        Calendar cal = Calendar.getInstance();
        cal.add(5, -1);
        System.out.print(cal.getTime());
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        CurrentTime = formatter.format(cal.getTime());
        return CurrentTime;
    }

    public static String getYesterdayCompactTimeForFileName() {
        Calendar cal = Calendar.getInstance();
        cal.add(5, -1);
        System.out.print(cal.getTime());
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        CurrentTime = formatter.format(cal.getTime());
        return CurrentTime;
    }

    public static String getCurrentDate() {
        Date nowDate = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        CurrentDate = formatter.format(nowDate);
        return CurrentDate;
    }

    public static String getCurrentDate1() {
        Date nowDate = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
        CurrentDate = formatter.format(nowDate);
        return CurrentDate;
    }

    public static String getCurrentFirstDate() {
        Date nowDate = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-01");
        CurrentDate = formatter.format(nowDate);
        return CurrentDate;
    }

    public static String getCurrentLastDate() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = null;

        try {
            Date date = formatter.parse(getCurrentFirstDate());
            calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(2, 1);
            calendar.add(6, -1);
            return formatDate(calendar.getTime());
        } catch (
                ParseException var3) {
            log.error(var3.getMessage());
            return "";
        }
    }

    public static String formatDate(Date date) {
        return formatDateByFormat(date, "yyyy-MM-dd");
    }

    public static String formatDateByFormat(Date date, String format) {
        String result = "";
        if (date != null) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat(format);
                result = sdf.format(date);
            } catch (
                    Exception var4) {
                log.error(var4.getMessage());
            }
        }

        return result;
    }

    public static String addDay(String currentdate, int addDay) {
        GregorianCalendar gc = null;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

        try {
            int year = Integer.parseInt(currentdate.substring(0, 4));
            int month = Integer.parseInt(currentdate.substring(5, 7)) - 1;
            int day = Integer.parseInt(currentdate.substring(8, 10));
            gc = new GregorianCalendar(year, month, day);
            gc.add(5, addDay);
            return formatter.format(gc.getTime());
        } catch (
                Exception var8) {
            log.error(var8.getMessage());
            return null;
        }
    }

    public static String addMonth(String currentdate, int addMonth) {
        GregorianCalendar gc = null;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

        try {
            int year = Integer.parseInt(currentdate.substring(0, 4));
            int month = Integer.parseInt(currentdate.substring(5, 7)) - 1;
            int day = Integer.parseInt(currentdate.substring(8, 10));
            gc = new GregorianCalendar(year, month, day);
            gc.add(2, addMonth);
            return formatter.format(gc.getTime());
        } catch (
                Exception var8) {
            log.error(var8.getMessage());
            return null;
        }
    }

    public static int monthDiff(String beforeTime, String endTime) {
        if (beforeTime != null && endTime != null) {
            try {
                int beforeYear = Integer.parseInt(beforeTime.substring(0, 4));
                int endYear = Integer.parseInt(endTime.substring(0, 4));
                int beforeMonth = Integer.parseInt(beforeTime.substring(5, 7)) - 1;
                int endMonth = Integer.parseInt(endTime.substring(5, 7)) - 1;
                return (endYear - beforeYear) * 12 + (endMonth - beforeMonth);
            } catch (
                    Exception var7) {
                log.error(var7.getMessage());
                return 0;
            }
        } else {
            return 0;
        }
    }

    public static String addMinute(String currentdatetime, int addMinute) {
        GregorianCalendar gc = null;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        try {
            int year = Integer.parseInt(currentdatetime.substring(0, 4));
            int month = Integer.parseInt(currentdatetime.substring(5, 7)) - 1;
            int day = Integer.parseInt(currentdatetime.substring(8, 10));
            int hour = Integer.parseInt(currentdatetime.substring(11, 13));
            int minute = Integer.parseInt(currentdatetime.substring(14, 16));
            int second = Integer.parseInt(currentdatetime.substring(17, 19));
            gc = new GregorianCalendar(year, month, day, hour, minute, second);
            gc.add(12, addMinute);
            return formatter.format(gc.getTime());
        } catch (
                Exception var11) {
            log.error(var11.getMessage());
            return null;
        }
    }

    public static String addSecond(String currentdatetime, int addSecond) {
        GregorianCalendar gc = null;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        try {
            int year = Integer.parseInt(currentdatetime.substring(0, 4));
            int month = Integer.parseInt(currentdatetime.substring(5, 7)) - 1;
            int day = Integer.parseInt(currentdatetime.substring(8, 10));
            int hour = Integer.parseInt(currentdatetime.substring(11, 13));
            int minute = Integer.parseInt(currentdatetime.substring(14, 16));
            int second = Integer.parseInt(currentdatetime.substring(17, 19));
            gc = new GregorianCalendar(year, month, day, hour, minute, second);
            gc.add(13, addSecond);
            return formatter.format(gc.getTime());
        } catch (
                Exception var11) {
            log.error(var11.getMessage());
            return null;
        }
    }

    public static String addMinute1(String currentdatetime, int addMinute) {
        GregorianCalendar gc = null;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");

        try {
            int year = Integer.parseInt(currentdatetime.substring(0, 4));
            int month = Integer.parseInt(currentdatetime.substring(5, 7)) - 1;
            int day = Integer.parseInt(currentdatetime.substring(8, 10));
            int hour = Integer.parseInt(currentdatetime.substring(8, 10));
            int minute = Integer.parseInt(currentdatetime.substring(8, 10));
            int second = Integer.parseInt(currentdatetime.substring(8, 10));
            gc = new GregorianCalendar(year, month, day, hour, minute, second);
            gc.add(12, addMinute);
            return formatter.format(gc.getTime());
        } catch (
                Exception var11) {
            log.error(var11.getMessage());
            return null;
        }
    }

    public static Date parseDate(String sDate) {
        SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        try {
            Date date = bartDateFormat.parse(sDate);
            return date;
        } catch (
                Exception var3) {
            log.error(var3.getMessage());
            return null;
        }
    }

    public static Date parseDateTime(String sDateTime) {
        SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        try {
            Date date = bartDateFormat.parse(sDateTime);
            return date;
        } catch (
                Exception var3) {
            log.error(var3.getMessage());
            return null;
        }
    }

    public static int getTotalDaysOfMonth(String strDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = new GregorianCalendar();
        Date date = new Date();

        try {
            date = sdf.parse(strDate);
        } catch (
                Exception var5) {
            log.error(var5.getMessage());
        }

        calendar.setTime(date);
        int day = calendar.getActualMaximum(5);
        return day;
    }

    public static long getDateSubDay(String startDate, String endDate) {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        long theday = 0L;

        try {
            calendar.setTime(sdf.parse(startDate));
            long timethis = calendar.getTimeInMillis();
            calendar.setTime(sdf.parse(endDate));
            long timeend = calendar.getTimeInMillis();
            theday = (timethis - timeend) / 86400000L;
        } catch (
                Exception var10) {
            log.error(var10.getMessage());
        }

        return theday;
    }

    public static String getDateToString(Date date) {
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf1.format(date);
    }

    // public static void main(String[] args) {
    //     System.out.println(addDay(getCurrentDate(), -1));
    // }

}
